/***************************************************************************
*   Copyright (C) 2006 by Yuri Ovcharenko                                 *
*   amwsoft@gmail.com                                                     *
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation; either version 2 of the License, or     *
*   (at your option) any later version.                                   *
*                                                                         *
*   This program is distributed in the hope that it will be useful,       *
*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
*   GNU General Public License for more details.                          *
*                                                                         *
*   You should have received a copy of the GNU General Public License     *
*   along with this program; if not, write to the                         *
*   Free Software Foundation, Inc.,                                       *
*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
***************************************************************************/
#ifndef RUNNERWIDGET_H
#define RUNNERWIDGET_H

#include <QFrame>
#include <QProcess>

/**
	@author Yuri Ovcharenko <amwsoft@gmail.com>
*/

class QSettings;
class QLineEdit;
class QPushButton;
class QCheckBox;
class QListWidget;

/**
 * @class RunnerWidget
 * @brief The main widget for @a Runner plugin.
 *
 * RunnerWidget implements user interface for @a Runner plugin.
 * The reference to this widget returned by @a DCPlugin::widget function.
 * The RunnerWidget must be used by main program to connect SIGNALS and SLOTS.
 * @sa Runner DCPlugin
 */
class RunnerWidget : public QFrame
{
		Q_OBJECT
	public:
		/**
		 * @RunnerWidget
		 * @brief Construntor.
		 */
		RunnerWidget( QWidget *parent = 0 );

		/**
		 * @~RunnerWidget
		 * @brief Destruntor.
		 */
		~RunnerWidget();

	public:

		/**
		 * @saveSettings
		 * @brief Saves the settings of plugin.
		 *
		 * This function is called by @a PluginManager to restore settings.
		 * Settings restored on plugin activation.
		 * @param settings	The QSettings object provided by @a Runner plugin to save settings.
		 * @sa restoreSettings
		 */
		void saveSettings(QSettings * settings);

		/**
		 * @restoreSettings
		 * @brief Restores the settings of plugin.
		 *
		 * This function is called by @a PluginManager to restore settings.
		 * Settings restored on plugin activation.
		 * @param settings	The QSettings object provided by @a Runner plugin to restore settings.
		 * @sa saveSettings
		 */
		void restoreSettings(QSettings * settings);

	private slots:
		void getProgName();
		void run();
		void log(const QString & msg);

		void processError(QProcess::ProcessError error);
		void processFinished(int exitCode, QProcess::ExitStatus);
		void processReadStdErr();
		void processReadStdOut();

	private:
		QLineEdit *leFileName;
		QPushButton *bBrowse;
		QLineEdit *leParams;
		QCheckBox *cbCloseOnRun;
		QCheckBox *cbAutoReopen;
		QPushButton *bRun;
		QProcess process;
		QListWidget *logList;

	signals:

		/**
		 * @atStart
		 *
		 * This signal is emited before starting program.
		 * @param needClose	If true the main program must close port on receiving this signal.
		 */
		void atStart(bool needClose);
		/**
		 * @stopped
		 *
		 * This signal is emited after program is exited.
		 * @param canOpen	If true the main program can reopen port.
		 */
		void stopped(bool canOpen);
};

#endif
